const app = getApp(); //引入导航

import { JqIndex } from 'jqIndex.model.js';
var jqindex = new JqIndex();

var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');

var qqmapsdk;
Page({
  data: {
    cityData: "全国",
    showIcon: true,
    //显示返回键以及首页
    scrollLeft: 0,
    SearchClose: true,
    SearchTo: false,
    searchFlag: true,
    //搜索
    Rhead: '',
    //上拉加载参数
    Rtail: '' //上拉加载参数

  },
  onLoad: function (options) {
    //判断打开页面是否携带城市参数
    if (options.city) {
      var city = options.city;
      var province = options.province;
      tt.setStorageSync('wxcity', city);
      tt.setStorageSync('wxprovince', province);

      this._cityget();

      console.log(city); //如果没携带就调用获取当前位置接口
    } else {
      console.log('定位');
      qqmapsdk = new QQMapWX({
        key: '5XNBZ-SFPRU-M35VS-BNF67-DIHHJ-AEFD7' //这里自己的key秘钥进行填充

      }); //获取当时用户小程序设置（权限设置状态）

      tt.getSetting({
        success: res => {
          if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
            this.getLocation();
          } else if (res.authSetting['scope.userLocation'] == undefined) {
            //调用wx.getLocation的API
            this.getLocation();
          } else {
            //调用wx.getLocation的API
            this.getLocation();
          }
        }
      });
    } //判断来源


    var source = options.source;

    if (source) {
      tt.setStorageSync('source', source);
    }

    var that = this; //轮播图

    jqindex.getRotary(res => {
      console.log(res);
      that.setData({
        bannerArr: res
      });
    });
  },
  //调起接口方法
  _cityget: function () {
    var head = 0;
    var tail = 6;
    tt.setStorageSync('Rhead', head);
    tt.setStorageSync('Rtail', tail);
    var that = this;
    tt.getStorage({
      key: 'wxcity',
      success: function (res) {
        console.log('获取到缓存');
        var province = tt.getStorageSync('wxprovince');
        var city = res.data;
        var type = 'i'; //每日推荐

        var taill = 10;
        jqindex.RecommendedDaily(city, province, type, head, taill, res => {
          console.log(res);
          that.setData({
            RecommendedDailyArr: res
          });
        });
        var type = 'd'; //热门抢购

        jqindex.RecommendedDaily(city, province, type, head, tail, res => {
          console.log(res);
          that.setData({
            HotToSnapUpArr: res
          });
        });
      },
      fail: function (res) {
        console.log('缓存获取不到');
        var province = '';
        var city = '';
        var type = 'i'; //每日推荐

        jqindex.RecommendedDaily(city, province, type, head, tail, res => {
          console.log(res);
          that.setData({
            RecommendedDailyArr: res
          });
        });
        var type = 'd'; //热门抢购

        jqindex.RecommendedDaily(city, province, type, head, tail, res => {
          console.log(res);
          that.setData({
            HotToSnapUpArr: res
          });
        });
      }
    });
  },
  // 微信获得经纬度
  getLocation: function () {
    let vm = this;
    tt.getLocation({
      type: 'wgs84',
      success: function (res) {
        var latitude = res.latitude;
        var longitude = res.longitude;
        vm.getLocal(latitude, longitude);
      },
      fail: function (res) {
        console.log('fail' + JSON.stringify(res));

        vm._cityget();
      }
    });
  },
  // 获取当前地理位置
  getLocal: function (latitude, longitude) {
    let vm = this;
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude
      },
      success: function (res) {
        console.log(res);
        let province = res.result.ad_info.province;
        let city = res.result.ad_info.city; //设置缓存（吧用户地址信息存入缓存）

        tt.setStorageSync('wxcity', city);
        tt.setStorageSync('wxprovince', province);
        vm.setData({
          province: province,
          city: city,
          latitude: latitude,
          longitude: longitude
        }); //显示2秒隐藏加载提示

        setTimeout(function () {
          vm._cityget();
        }, 1000);
      },
      fail: function (res) {
        console.log("error");
      },
      complete: function (res) {}
    });
  },
  //更多跳转景区列表
  SpecialOffer: function () {
    tt.removeStorage({
      key: '_cateList',

      success(res) {
        console.log(res.data);
      }

    });
    tt.removeStorage({
      key: '_cateId',

      success(res) {
        tt.switchTab({
          url: '../jqList/jqList',
          //点击跳转到列表页
          success: function () {
            var page = getCurrentPages().pop();
            if (page == undefined || page == null) return;
            page.onLoad();
          }
        });
      }

    });
  },
  //页面跳转
  jump: function (e) {
    var id = e.currentTarget.dataset.id;
    tt.navigateTo({
      url: '../jqdetail/jqdetail?id=' + id
    });
  },
  //关闭页面时隐藏城市弹框,关闭一级和二级菜单
  onShow: function () {},
  //上拉加载方法
  onReachBottom: function (e) {
    var that = this;
    var city = tt.getStorageSync('wxcity');
    var Rhead = tt.getStorageSync('Rhead') + 6;
    var province = tt.getStorageSync('wxprovince');
    var tail = 6;
    var type = 'd'; //热门抢购

    jqindex.RecommendedDaily(city, province, type, Rhead, tail, res => {
      console.log('上拉加载~~~~~~~', res);
      that.setData({
        HotToSnapUpArr: that.data.HotToSnapUpArr.concat(res)
      });
      tt.setStorageSync('Rhead', Rhead);
    });
  },
  //点击分类跳转到列表页
  cateClick: function (e) {
    console.log(e);
    var _cateId = e.currentTarget.dataset.cate; //获取到cateId

    var _cateList = e.currentTarget.dataset.list; //首页传到列表页，列表页获取到值滚动距离

    var _city = '';
    tt.setStorageSync('_city', _city);
    tt.setStorageSync('_cateId', _cateId);
    tt.setStorageSync('_cateList', _cateList);

    if (_cateList == 0) {
      tt.switchTab({
        url: '../yclist/yclist',
        //点击跳转到列表页
        success: function () {
          var page = getCurrentPages().pop();
          if (page == undefined || page == null) return;
          page.onLoad();
        }
      });
    } else {
      tt.switchTab({
        url: '../jqList/jqList',
        //点击跳转到列表页
        success: function () {
          var page = getCurrentPages().pop();
          if (page == undefined || page == null) return;
          page.onLoad();
        }
      });
    }
  },
  //打开搜索
  searchValue: function (e) {
    console.log(e);
    var _data = e.detail.value;
    this.data.SearchValue = e.detail.value;
    var patt3 = /^[\u4e00-\u9fa5]{2,20}$/; //正则验证汉字不能少两个，不能多余20个 

    this.setData({
      //保存输入框的值
      SearchVal: e.detail.value
    });
    jqindex.getSearchFor(this.data.SearchVal, res => {
      console.log(res);
      this.setData({
        SearchList: res
      });
    });
    this.data.datarow = _data;

    if (_data == '') {
      this.setData({
        searchFlag: true,
        SearchClose: true,
        SearchTo: false,
        SearchVal: "" //清空输入框的值

      });
    } else if (_data != '') {
      //搜索页面
      this.setData({
        searchFlag: false,
        SearchClose: false,
        SearchTo: true,
        datarow: this.data.datarow
      });
    }
  },
  //关闭搜索
  onCloseSearch: function () {
    this.setData({
      searchFlag: true,
      SearchClose: true,
      SearchTo: false,
      SearchVal: "" //清空输入框的值

    });
  },
  //使用回车键搜索
  searchEnter: function () {
    this.setData({
      searchFlag: true,
      SearchClose: true,
      SearchTo: false,
      SearchVal: "" //清空输入框的值

    });
    var inputdata = this.data.SearchValue;

    if (inputdata) {
      tt.navigateTo({
        url: '../ycSearch/ycSearch?inputdata=' + inputdata
      });
    }
  },
  //点击关闭按钮关闭搜索框
  toClose: function () {
    this.setData({
      searchFlag: true,
      //关闭搜索列表
      SearchClose: true,
      //打开关闭搜索图标
      SearchTo: false,
      //关闭搜索图标
      SearchVal: "" //清空输入框的值

    });
  }
});